package com.yuedong.mvvm.ui.home;


import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuedong.base.util.ToastUtil;
import com.yuedong.mvvm.R;
import com.yuedong.mvvm.base.BaseFrament;
import com.yuedong.mvvm.model.ErrorModel;
import com.yuedong.mvvm.model.ResponseModel;
import com.yuedong.mvvm.model.VersionBean;
import com.yuedong.mvvm.viewmodel.HomeFViewModel;
import com.yuedong.mvvm.viewmodel.repository.state.LoadingState;
import com.yuedong.view.stateview.SuccessState;


import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFrament<HomeFViewModel> {
    @BindView(R.id.tvText)
    TextView tvText;
    @BindView(R.id.lcontainer)
    LinearLayout lcontainer;

    @Override
    protected int initLayout(Bundle bundle) {
        return R.layout.fragment_home;
    }


    @Override
    protected void initData(Bundle bundle) {
        // registerStateView(lcontainer);
        // showError("2");
        viewModel.getmRespository().getResponse().observe(this, new Observer<ResponseModel>() {
            @Override
            public void onChanged(@Nullable ResponseModel responseModel) {
                Log.v("fdsfsdfsfsfsfe", responseModel.tag);
            }
        });

    }

    @Override
    protected HomeFViewModel getViewModel() {
        return ViewModelProviders.of(this).get(HomeFViewModel.class);
    }

    @Override
    protected void onStateRefresh() {
        super.onStateRefresh();
        loadManager.showStateView(LoadingState.class);
        viewModel.getVersion().observe(this, new Observer<VersionBean>() {
            @Override
            public void onChanged(@Nullable VersionBean versionBean) {
                tvText.setText(versionBean.toString());
                loadManager.showStateView(SuccessState.class);
            }
        });
    }

    int i = 0;

    @OnClick({R.id.btGo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btGo:
               //  viewModel.getmRespository().sendEvent(new ErrorModel(0,"err"));
                //  startActivity(new Intent(getActivity(), HomeOneActivity.class));
                if (i == 0) viewModel.getmRespository().sendData("a", new ArrayList<>());
                else viewModel.getmRespository().sendData("b", "ggggggggg");
                i += 1;
                break;
        }
    }
}
